/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.UserAgentUtils;

/**
 * 手机端视图拦截器
 * @author ThinkGem
 * @version 2014-9-1
 */
public class MobileInterceptor extends BaseService implements HandlerInterceptor {
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
			Object handler) throws Exception {
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, 
			ModelAndView modelAndView) throws Exception {
		if (modelAndView != null){
			// 如果是手机或平板访问的话，则跳转到手机视图页面。
			if(UserAgentUtils.isMobileOrTablet(request) && !StringUtils.startsWithIgnoreCase(modelAndView.getViewName(), "redirect:")){
				
				// mod by zad 2018-02-27 手机后者平板访问时，路径不在添加/mobile
				// modelAndView.setViewName("mobile/" + modelAndView.getViewName());
				
				String url = modelAndView.getViewName();
				// 登录页面为同一登录页面，不做过滤处理
				if(url.indexOf("gt/basic/login") < 0 && !url.endsWith("List") && !url.endsWith("Form") 
						&& !url.endsWith("save") && !url.endsWith("chart") && !url.endsWith("gtModifyPwd")
						&& !url.endsWith("exportDBIndex")){
					if(url.indexOf("/basic/") > -1)
					{
						url = url.replace("/basic/", "/weixin/");
					}
					modelAndView.setViewName(url);
				}
				 
				// 记录手机端访问
				modelAndView.addObject("loginType", "weixin");
				// ---手机或者平板---:modules/gt/basic/index
				System.out.println("---手机或者平板---:" + modelAndView.getViewName());
			}
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, 
			Object handler, Exception ex) throws Exception {
		
	}

}
